package view;

import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Arrays;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import javax.swing.border.EmptyBorder;

public class Login extends JFrame implements ActionListener{
	//private JTable table;
	private JTextField tfUserName;
	private JPasswordField pf;
	private JButton btLogin,btExit;
	String actionLogin="login";
	String actionExit="exit";
	public Login(){
		add(createMainPanel());
		//set display
		setTitle("Login");
		setDefaultCloseOperation(EXIT_ON_CLOSE);
		pack();
		setLocationRelativeTo(null);
		setVisible(true);
	}
	private JPanel createMainPanel(){
		JPanel panel=new JPanel(new BorderLayout());
		panel.setBorder(new EmptyBorder(10, 10, 10, 10));
		panel.add(createInputPanel(),BorderLayout.CENTER);
		panel.add(createButtonPanel(),BorderLayout.PAGE_END);
		return panel;
	}
	private JPanel createInputPanel(){
		int col=15;
		JPanel panel=new JPanel(new GridLayout(2, 2, 5, 5));
		panel.add(createLabel("Username"));
		panel.add(tfUserName=createTextField(col));
		panel.add(createLabel("Password"));
		panel.add(pf=createPasswordField(actionLogin,col));
		return panel;
	}
	private JPanel createButtonPanel(){
		JPanel panel=new JPanel();
		panel.add(btLogin=createButton(actionLogin,"Login"));
		panel.add(btExit=createButton(actionExit,"Exit"));
		return panel;
	}
	private JLabel createLabel(String text){
		JLabel lb=new JLabel(text);
		return lb;
	}
	private JTextField createTextField(int col){
		JTextField tf=new JTextField(col);
		return tf;
	}
	private JPasswordField createPasswordField(String action,int col){
		JPasswordField pf=new JPasswordField(col);
		pf.setActionCommand(action);
		pf.addActionListener(this);
		return pf;
	}
	private JButton createButton(String action,String text){
		JButton bt=new JButton(text);
		bt.setActionCommand(action);
		bt.addActionListener(this);
		return bt;
	}
	//check password
	private boolean checkData(String inputUsername,char[] inputPassword){
		String user="CongHV";
		char[] password={'1','1','9','1','9','9','3'};
		return(Arrays.equals(inputPassword, password)&&user.equals(inputUsername));
	}
	
	@Override
	public void actionPerformed(ActionEvent e) {
		String command=e.getActionCommand();
		if(actionLogin.equals(command)){
			if(checkData(tfUserName.getText(), pf.getPassword())){
				JOptionPane.showMessageDialog(null, "Login success!");
				setVisible(false);
				new MenuForm().setVisible(true);
			}else{
				JOptionPane.showMessageDialog(null, "Login failed");
			}
			return;
		}
		
		
		if(actionExit.equals(command)){
			System.exit(0);
			
		}
	}
//	public static void main(String[] args) {
//		new Login();
//	}
}
